package web.servlet;


import base.BaseServlet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import pojo.Course;
import service.CourseService;
import service.imp.CourseServiceImpl;
import utils.DateUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/course")
public class CourseServlet extends BaseServlet {
    //查询课程信息列表
    public void findCourseList(HttpServletRequest request, HttpServletResponse response){


        try {
            CourseService cs = new CourseServiceImpl();
            List<Course> courseList = cs.findCourseList();

            //响应结果
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id","course_name","price","sort_num","status");
            String jsonString = JSON.toJSONString(courseList,filter);
            response.getWriter().print(jsonString);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //根据条件查询课程信息
    public void findByCourseNameAndStatus(HttpServletRequest request, HttpServletResponse response){
        try {
            String course_name = request.getParameter("course_name");
            String status = request.getParameter("status");

            CourseService courseService = new CourseServiceImpl();
            List<Course> courseList = courseService.findByCourseNameAndStatus(course_name, status);

            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id","course_name","price","sort_num","status");
            String jsonString = JSON.toJSONString(courseList,filter);
            response.getWriter().print(jsonString);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //根据ID查询课程信息
    public void findCourseById(HttpServletRequest request, HttpServletResponse response){
        try {
            String id = request.getParameter("id");

            CourseService courseService = new CourseServiceImpl();
            Course course = courseService.findCourseById(Integer.parseInt(id));

            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id","course_name","brief","teacher_name",
                    "teacher_info","preview_first_field","preview_second_field","discounts","price","price_tag","share_image_title","share_title","share_description","course_description");

            String result = JSON.toJSONString(course,filter);

            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //修改课程状态
    public void updateCourseStatus(HttpServletRequest request, HttpServletResponse response){
        try {
            String id = request.getParameter("id");

            CourseService courseService = new CourseServiceImpl();
            Course course = courseService.findCourseById(Integer.parseInt(id));

            int status = course.getStatus();

            if (status ==0){
                course.setStatus(1);
            }else {
                course.setStatus(0);
            }

            course.setUpdate_time(DateUtils.getDateFormart());


            Map<String, Integer> map = courseService.updateCourseStatus(course);

            String result = JSON.toJSONString(map);

            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
